package idv.ccApp.ChartDemo;

import android.app.Activity;
import android.graphics.Paint;
import android.os.Bundle;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.BarChart;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import java.util.LinkedList;
import java.util.List;

public class ChartDemoMainActivity extends Activity {

  List<XYSeries> mListXYSerial = new LinkedList<XYSeries>();
  double [] m102MouthAvgTemperArray = {15.2,	17.3,	21.0,	24.3,	26.7,	28.4,	28.2,	26.6,	23.3,	20.2, 17.0};
  double [] m101MouthAvgTemperArray = {14.2,	18.3,	22.3,	24.5,	26.9,	28.1,	28.3,	26.7,	22.9,	20.4, 16.7};
  String [] mRenderName = { "Y101", "Y102"};
  String [] mMonthArray = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
  int [] mColorAray = {Color.YELLOW, Color.BLUE, Color.CYAN, Color.DKGRAY, Color.GRAY, Color.GREEN,
                       Color.LTGRAY, Color.MAGENTA, Color.RED, Color.WHITE, Color.BLACK, Color.argb(33, 33, 33, 33)
  };

  private ChartFactory mChart;

  XYMultipleSeriesDataset mXYMultiSeriesDataSet = new XYMultipleSeriesDataset();
  XYMultipleSeriesRenderer mXYMultiSeriesRender = new XYMultipleSeriesRenderer();

  private void createXYDataSerial( double[] data){
    XYSeries newXY = new XYSeries("102 Avg. Temperature");
    for (int i=0; i<data.length; i++)
      newXY.add(i, data[i]);
    this.mListXYSerial.add(newXY);
    this.mXYMultiSeriesDataSet.addSeries(newXY);
  }

  private void createXYRender( int numberOfRender, int [] colorArray) {
    for ( int i=0; i<numberOfRender; i++){
      XYSeriesRenderer newRender = new XYSeriesRenderer();
      newRender.setColor(colorArray[i]);
      this.mXYMultiSeriesRender.addSeriesRenderer(i, newRender);
    }
  }

  private void settingXYMultiSeriesRender(){
    this.mXYMultiSeriesRender.setXTitle("Month");
    this.mXYMultiSeriesRender.setYTitle("Temperature");
    this.mXYMultiSeriesRender.setXLabelsAlign(Paint.Align.CENTER);
    this.mXYMultiSeriesRender.setYLabelsAlign(Paint.Align.CENTER);
    this.mXYMultiSeriesRender.setXAxisMin(0);
    this.mXYMultiSeriesRender.setXAxisMax(12);
    this.mXYMultiSeriesRender.setYAxisMin(10.0);
    this.mXYMultiSeriesRender.setYAxisMax(30.0);
    this.mXYMultiSeriesRender.setXLabelsColor(Color.GREEN);

    for (int i=0; i<this.mMonthArray.length; i++){
      this.mXYMultiSeriesRender.addXTextLabel(i+1, this.mMonthArray[i]);
    }
  }

  @Override
  protected void onResume() {
    super.onResume();
    this.createXYDataSerial(this.m101MouthAvgTemperArray);
    this.createXYDataSerial(this.m102MouthAvgTemperArray);
    this.createXYRender(2, this.mColorAray);

    View v = ChartFactory.getBarChartView(this, this.mXYMultiSeriesDataSet, this.mXYMultiSeriesRender, BarChart.Type.DEFAULT);
    setContentView(v);
  }

  /**
   * Called when the activity is first created.
   */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
  }

}
